//
//  ReSetPassController2.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/25.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "CKPuslichTool.h"
#import "DBAcquireAuthCodeButton.h"
#import "ReSetPassController2.h"
#import "PassSetController.h"
#import "HandleNetLogin.h"
#import "UserModel.h"
@interface ReSetPassController2 ()
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet DBAcquireAuthCodeButton *codeButton;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UIButton *NextBt;
@property (weak, nonatomic) IBOutlet UILabel *titleLa;
@property (strong,nonatomic)NSDate *oldDate;
@property (strong,nonatomic)NSString *mCode;
@end

@implementation ReSetPassController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.codeTF.layer.cornerRadius=5;
    self.codeButton.layer.cornerRadius=5;
    self.NextBt.layer.cornerRadius=5;
    self.reminder1.text=@"";
    if ([_whereFrom isEqualToString:@"reSetPass"]) {
        self.titleLa.text=@"重置密码(2/3)";
    }else{
        self.titleLa.text=@"重置手势密码";
    }
    
    
    [self MoveTextfileContent:self.codeTF];
    [self.codeTF   addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    // Do any additional setup after loading the view.
}
- (void)textFieldDidChange:(UITextField *)textField
{

        if (textField.text.length>50) {
            textField.text = [textField.text substringToIndex:50];
        }

    
}
-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)tarStartButtonTimers:(id)sender{
    
        [HandleNetLogin retrieveCodeWithUserName:self.userinfo[@"name"] AccountPhone:self.userinfo[@"phone"] Success:^(NSDictionary *data) {
            _mCode = data[@"VerificationCode"];;
            _oldDate=[NSDate date];
            [_codeButton tarStartButtonTimer];
        } Failure:^(NSString *errorInfo) {
            self.reminder1.text=[NSString stringWithFormat:@"*%@",errorInfo];
        }];

    


}

- (IBAction)goNext:(id)sender {
    
    if (self.codeTF.text.length == 0) {
        self.reminder1.text=@"请输入验证码";
        return;
    }
    if (![self.codeTF.text isEqualToString:_mCode]) {
        self.reminder1.text=@"验证码验证失败";
        return;
    }
    
    NSDate *currentDate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate *localDate1 = [currentDate dateByAddingTimeInterval:interval];
    NSDate *localDate2 = [_oldDate dateByAddingTimeInterval:interval];
    
    //计算时间差间隔
    NSTimeInterval timeBetween = [localDate1 timeIntervalSinceDate:localDate2];
    NSLog(@"timeBetween---%f",timeBetween);
    if (timeBetween>60*10) {
        self.reminder1.text=@"验证码已失效";
        return;
    }else{
        if ([_whereFrom isEqualToString:@"reSetPass"]) {
            PassSetController *vc=(PassSetController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PassSetController"];
            vc.isFrome=@"ReSet";
            vc.userinfo=self.userinfo;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
             [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PICPASSCODE"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    }
    
    
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
